package manager.cms.service.exec;
import common.SnowflakeIdWorker;
import manager.cms.service.JdExecutor;
import manager.feign.GoodsListThirdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 获取优惠卷信息
 */
@Service(value = "itemListExec")
public class ItemListExecImpl implements JdExecutor {
    @Autowired
    private GoodsListThirdService goodsListThirdService;
    private SnowflakeIdWorker snowflakeIdWorker;
    private List<String> skuIds;

    public List<String>  getSkuIds(){
         return this.skuIds.stream().distinct().collect(Collectors.toList());
    }
    public void execute(Map<String,Object> mapParams) {
        try {
            List<String> categoryIds=(List<String>)mapParams.get("categoryIds");
            String[] categoryStrIds = new String[categoryIds.size()];
            categoryIds.toArray(categoryStrIds);
            Integer page=(Integer) mapParams.get("page");
            String  itemListHtml=goodsListThirdService.getGoodsList(categoryStrIds,page);
            String pattern = "li data-sku=\".*?\"";
            Pattern compile = Pattern.compile(pattern);
            Matcher m=compile.matcher(itemListHtml);
            String spuId="";
            this.skuIds=new ArrayList<String>();
            while (m.find()){
                spuId=m.group();
                this.skuIds.add(spuId.substring(13,spuId.length()-1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
